package com.feibi.study.NetWork;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.feibi.study.Utils.Global;
import com.feibi.study.Utils.PreferencesUtil;
import com.feibi.study.Utils.JsonUtil;

import java.util.HashMap;

public class BasicModule {
    public static LogOutCallback logOutCallback;

    public static void setLogOutCallback(LogOutCallback logOutCallback) {
        BasicModule.logOutCallback = logOutCallback;
    }

    public Context context;

    public BasicModule(Context context) {
        this.context = context;
    }

    public static String DO_MAIN = "https://heart.dhkzw.top/";

    public <T> void get(Context context, String url, Object req, final HttpCallBack<BasicResponseBody<T>> reqCallBack, final Class<T> entityClass) {
        HttpRequestManager requestManager = HttpRequestManager.getInstance(context);
        HashMap<String, Object> paramsMap = JsonUtil.jsonToMap(JSON.toJSONString(req));
        requestManager.requestAsyn(DO_MAIN + url, HttpRequestManager.TYPE_GET, paramsMap, getHeaderMap(context), getHttpCallBack(reqCallBack, entityClass));
    }

    public <T> void post(Context context, String url, Object req, final HttpCallBack<BasicResponseBody<T>> reqCallBack, final Class<T> entityClass) {
        HttpRequestManager requestManager = HttpRequestManager.getInstance(context);
        HashMap<String, Object> paramsMap = JsonUtil.jsonToMap(JSON.toJSONString(req));
        requestManager.requestAsyn(DO_MAIN + url, HttpRequestManager.TYPE_POST_FORM, paramsMap, getHeaderMap(context), getHttpCallBack(reqCallBack, entityClass));
    }

//    public <T> void fileUpload(Context context, String url, UploadFileReq req, final HttpCallBack<BasicResponseBody<T>> reqCallBack, final Class<T> entityClass) {
//        HttpRequestManager requestManager = HttpRequestManager.getInstance(context);
//        HashMap<String, Object> paramsMap = new HashMap<>();
//        if (req.getFile() == null) {
//            if (reqCallBack != null) {
//                BasicResponseBody<T> response = new BasicResponseBody<T>();
//                response.setCode("666");
//                response.setMsg("no files");
//                reqCallBack.onReqSuccess(response);
//            }
//            return;
//        }
//        paramsMap.put("file", req.getFile());
//        paramsMap.put("type", req.getType());
//        requestManager.upLoadFile(DO_MAIN + url, paramsMap, getHeaderMap(context), getHttpCallBack(reqCallBack, entityClass));
//    }

    public <T> void postJson(Context context, String url, Object req, final HttpCallBack<BasicResponseBody<T>> reqCallBack, final Class<T> entityClass) {
        HttpRequestManager requestManager = HttpRequestManager.getInstance(context);
        HashMap<String, Object> paramsMap = JsonUtil.jsonToMap(JSON.toJSONString(req));
        requestManager.requestAsyn(DO_MAIN + url, HttpRequestManager.TYPE_POST_JSON_OBJ, paramsMap, getHeaderMap(context), getHttpCallBack(reqCallBack, entityClass));
    }

    public <T> void postCheckLogin(Context context, String url, Object req, final HttpCallBack<BasicResponseBody<T>> reqCallBack, final Class<T> entityClass) {
        if (!Global.Companion.isLogin()) {
            return;
        }
        HttpRequestManager requestManager = HttpRequestManager.getInstance(context);
        HashMap<String, Object> paramsMap = JsonUtil.jsonToMap(JSON.toJSONString(req));
        requestManager.requestAsyn(DO_MAIN + url, HttpRequestManager.TYPE_POST_FORM, paramsMap, getHeaderMap(context), getHttpCallBack(reqCallBack, entityClass));
    }

    private HashMap<String, Object> getHeaderMap(Context context) {
        HashMap<String, Object> headerMap = new HashMap<>();
        String token = PreferencesUtil.getString(context, "token");
        if (!TextUtils.isEmpty(token)) {
            headerMap.put("token", token);
        }
        headerMap.put("Content-Type", "application/json");
        return headerMap;
    }

    private <T> HttpCallBack<String> getHttpCallBack(final HttpCallBack<BasicResponseBody<T>> reqCallBack, final Class<T> entityClass) {
        return result -> {
            BasicResponseBody<T> response = null;
            try {
                response = JsonUtil.fromJson(result, entityClass);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (reqCallBack == null) {
                return;
            }
            if (response == null) {
                try {
                    response = JsonUtil.fromJson(result, Object.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (response != null) {
                if (!TextUtils.isEmpty(response.getNew_token())) {//更新token
                    PreferencesUtil.putString(context, "token", response.getNew_token());
                }
                if ("202".equals(response.getCode()) || "203".equals(response.getCode())) {
                    response.setMsg("");
                    toLoginPage();
                }
            } else {
                response = new BasicResponseBody<T>();
                response.setCode("600");
                response.setMsg("數據異常:response==null");
            }
            reqCallBack.onReqSuccess(response);
        };
    }

    public void toLoginPage() {
        if (Global.Companion.isLoginPage()) {
            return;
        }
        logOutCallback.LogOut(context);
    }

}

